/**
 * 智慧小区管理系统 - 物业信息管理模块API接口
 * 严格按照《Element Plus组件编码规范权威标准声明.md》实现
 * 
 * @author Wu.Liang
 * @date 2025-01-30
 * @version 1.0.1 - 添加has-company接口，解决路径冲突问题
 */

import api from '@/api/request'
import type { ApiResponse } from '@/api/request'

// 物业信息相关类型定义
export interface PropertyInfo {
  propertyCompanyId: number
  companyName: string
  companyCode?: string
  creditCode?: string
  businessLicense?: string
  legalRepresentative?: string
  establishedDate?: string
  registeredCapital?: number
  qualificationLevel?: string
  contactPerson?: string
  contactPhone?: string
  contactEmail?: string
  email?: string
  address?: string
  businessHours?: string
  serviceHotline?: string
  serviceScope?: string
  serviceArea?: string
  serviceCommunityCount?: number
  serviceHouseholdCount?: number
  serviceAreaSize?: number
  paymentAccount?: string
  remark?: string
  companyLogo?: string
  companyImages?: string[]
  createTime?: string
  updateTime?: string
  createBy?: string
  updateBy?: string
  status?: number
}

// 物业信息更新DTO
export interface PropertyInfoUpdateDTO {
  companyName: string
  companyCode?: string
  creditCode?: string
  businessLicense?: string
  legalRepresentative?: string
  establishedDate?: string
  registeredCapital?: number
  qualificationLevel?: string
  contactPerson?: string
  contactPhone?: string
  contactEmail?: string
  email?: string
  address?: string
  businessHours?: string
  serviceHotline?: string
  serviceScope?: string
  serviceArea?: string
  serviceCommunityCount?: number
  serviceHouseholdCount?: number
  serviceAreaSize?: number
  paymentAccount?: string
  remark?: string
}

// 物业统计信息
export interface PropertyStats {
  totalServiceArea: number
  serviceCommunityCount: number
  serviceHouseholdCount: number
  averageServiceQuality: number
  monthlyIncome: number
  monthlyExpense: number
  profitRate: number
  propertyCompanyId: number
  statisticsTime: number
}

// 物业信息管理API
export const propertyInfoApi = {
  /**
   * 获取物业公司基本信息
   * 
   * @param propertyCompanyId 物业公司ID
   * @returns 物业公司基本信息
   */
  getPropertyInfo: (propertyCompanyId: number): Promise<ApiResponse<PropertyInfo>> => {
    return api.get<ApiResponse<PropertyInfo>>(`/property/info/${propertyCompanyId}`)
  },

  /**
   * 获取物业公司综合统计信息
   * 包含核心统计数据（总服务面积、服务社区、服务户数）和物业特有数据（服务质量、月收入、月支出、利润率）
   * 
   * @param propertyCompanyId 物业公司ID
   * @returns 综合统计信息
   */
  getPropertyInfoStats: (propertyCompanyId: number): Promise<ApiResponse<PropertyStats>> => {
    return api.get<ApiResponse<PropertyStats>>(`/property/info/stats/${propertyCompanyId}`)
  },

  /**
   * 更新物业公司信息
   * 
   * @param propertyCompanyId 物业公司ID
   * @param data 更新数据
   * @returns 更新结果
   */
  updatePropertyInfo: (propertyCompanyId: number, data: PropertyInfoUpdateDTO): Promise<ApiResponse<boolean>> => {
    return api.put<ApiResponse<boolean>>(`/property/info/${propertyCompanyId}`, data)
  },

  /**
   * 获取当前用户物业公司信息
   * 
   * @returns 当前用户关联的物业公司信息
   */
  getCurrentUserPropertyInfo: (): Promise<ApiResponse<PropertyInfo>> => {
    return api.get<ApiResponse<PropertyInfo>>('/property/info/current-user')
  },

  /**
   * 获取当前用户物业公司统计信息
   * 
   * @returns 当前用户关联的物业公司统计信息
   */
  getCurrentUserPropertyInfoStats: (): Promise<ApiResponse<PropertyStats>> => {
    return api.get<ApiResponse<PropertyStats>>('/property/info/current-user/stats')
  },

  /**
   * 检查当前用户是否有关联的物业公司
   * 
   * @returns 是否有关联的物业公司
   */
  hasPropertyCompany: (): Promise<ApiResponse<boolean>> => {
    return api.get<ApiResponse<boolean>>('/property/info/has-company')
  },

  /**
   * 获取物业公司仪表盘统计数据（纯统计数据）
   * 注意：此接口专门用于获取核心统计数据，不包含物业特有数据
   * 
   * @param propertyCompanyId 物业公司ID
   * @returns 核心统计信息
   */
  getDashboardStats: (propertyCompanyId: number): Promise<ApiResponse<PropertyStats>> => {
    return api.get<ApiResponse<PropertyStats>>(`/property/statistics/dashboard/${propertyCompanyId}`)
  },

  /**
   * 获取当前用户物业公司仪表盘统计数据
   * 
   * @returns 当前用户物业公司核心统计信息
   */
  getCurrentUserDashboardStats: (): Promise<ApiResponse<PropertyStats>> => {
    return api.get<ApiResponse<PropertyStats>>('/property/statistics/dashboard/current-user')
  }
}

export default propertyInfoApi
